#ifndef _BLUETOOTH_APP_H_
#define _BLUETOOTH_APP_H_

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>

// How often to perform periodic event
#define SBP_PERIODIC_EVT_PERIOD                             (1600)

// How often to perform read rssi event
#define SBP_READ_RSSI_EVT_PERIOD                            (3200)

// Parameter update delay
#define SBP_PARAM_UPDATE_DELAY                              (6400)

// PHY update delay
#define SBP_PHY_UPDATE_DELAY                                (2400ul)

// What is the advertising interval when device is discoverable (units of 625us, 80=50ms)
#define DEFAULT_ADVERTISING_INTERVAL                        (80ul)

// Limited discoverable mode advertises for 30.72s, and then stops
// General discoverable mode advertises indefinitely
#define DEFAULT_DISCOVERABLE_MODE                           GAP_ADTYPE_FLAGS_GENERAL

// Minimum connection interval (units of 1.25ms, 10=12.5ms)
#define DEFAULT_DESIRED_MIN_CONN_INTERVAL                   (6)

// Maximum connection interval (units of 1.25ms, 100=125ms)
#define DEFAULT_DESIRED_MAX_CONN_INTERVAL                   (1000)

// Slave latency to use parameter update
#define DEFAULT_DESIRED_SLAVE_LATENCY                       (0ul)

// Supervision timeout value (units of 10ms, 100=1s)
#define DEFAULT_DESIRED_CONN_TIMEOUT                        (100ul)

// 默认判断接收完毕时间 - 1s
#define DEFAULT_BLUETOOTH_RECIVE_TIMEOUT                    (1000ul)
// 默认蓝牙发送延时 - 60ms
#define DEFAULT_BLUETOOTH_TRANSMIT_DELAY                    (60ul)

/**
 * @brief Peripheral Task Events
 * 
 */
typedef enum
{
  BLUETOOTH_START_DEVICE_EVENT       = 1 << 0,
  BLUETOOTH_READ_RSSI_EVENT          = 1 << 2,
  BLUETOOTH_PARAM_UPDATE_EVENT       = 1 << 3,
  BLUETOOTH_PHY_UPDATE_EVENT         = 1 << 4,
  BLUETOOTH_RECIVE_TIMEOUT_EVENT     = 1 << 5,
  BLUETOOTH_CONNECT_TIMEOUT_EVENT    = 1 << 6,
  BLUETOOTH_SEND_DATA_TO_HOST_EVENT  = 1 << 7,
} PeripheralTaskEvent_t;

/**
 * @brief 
 * 
 */
typedef struct
{
  uint16_t ConnHandle;       // 
  uint16_t ConnTimeout;      // 
  uint16_t ConnInterval;     // 
  uint16_t ConnSlaveLatency; // 
} PeripheralConnItem_t;

/**
 * @brief 获取蓝牙 MAC 地址编码
 * 
 * @param pMacAddrOut - MAC 地址编码
 */
extern void BluetoothGetMacAddr(uint8_t *pMacAddrOut);

/**
 * @brief 更新蓝牙广播包
 * 
 */
extern void BluetoothUpdatingTheBroadcastPackage(void);

/**
 * @brief 向主机发送一定长度的数据
 * 
 * @param pDataIn  - 待发送的数据
 * @param DataSize - 发送数据有效长度
 * @return 发送状态（true：成功 false：失败）
 */
extern bool BluetoothSendDataToHost(uint8_t *pDataIn, uint16_t DataSize);

/**
 * @brief 初始化蓝牙相关功能
 * 
 */
extern void BluetoothPeripheralInit(void);

#endif
